<template>
  <div class="login">
    <div class="title"><img src="@/assets/logo/logo.png" alt="">盛京金控资产运营管理平台</div>
    <el-form ref="loginForm" label-position="top" :model="loginForm" :rules="loginRules" class="login-form">
      <div v-show="!isforget">
        <div class="titlei">欢迎登录</div>
        <el-form-item prop="username" label="登录账号">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="登录密码">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled" label="验证码">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 65%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          </el-col>
          <el-col :span="12" style="text-align: right">
            <el-link type="info" @click="isforget = true">忘记密码</el-link>
          </el-col>
        </el-row>

        <el-form-item style="width:100%;margin-bottom: 0">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;border-radius: 10px;height: 60px;font-size: 26px"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </div>
      <div class="forget" v-show="isforget">
        <div class="titlef">忘记密码请扫码联系管理员找回</div>
        <img src="@/assets/images/qrcode.png" alt="">
        <el-link type="info" @click="isforget = false">返回登录</el-link>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      isforget: false,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  position: relative;
  width: 100%;
  height: 100%;
  background: url("../assets/images/bg.png") center center;
  background-size: 100% 100%;

}
.title {
  display: flex;
  width: 100%;
  position: absolute;
  top: 50px;
  left: 75px;
  align-items: center;
  font-weight: bold;
  font-size: 49px;
  color: #000221;
  img {
    width: 65px;
    height: 65px;
    margin-right: 10px;
  }
}
.titlei {
  font-size: 56px;
  color: #4076F6;
  font-family: PangMenZhengDao;
}
.forget {
  height: 540px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .titlef {
    font-weight: bold;
    text-align: center;
    font-size: 24px;
  }
  img {
    width: 400px;
    height: 400px;
    margin-bottom: 20px;
  }
}

.login-form {
  position: absolute;
  top: 50%;
  right: 25%;
  transform: translate(50%, -50%);
  border-radius: 20px;
  background: #ffffff;
  width: 600px;
  padding: 20px 60px 60px;
  ::v-deep {
    .el-form-item__label {
      font-size: 24px;
    }
    .el-input {
      height: 50px;
      .el-input__inner {
        border: 2px solid #D8E4FF;
        border-radius: 10px;
        line-height: 60px;
        padding-left: 40px;
        font-size: 24px;
      }
      input {
        height: 60px;
      }
    }
    .input-icon {
      height: 30px;
      width: 30px;
      margin-left: 5px;
      margin-top: 15px;
      color: #D8E4FF;
    }

  }

}
.login-tip {
  font-size: 22px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 60px;
  float: right;
  overflow: hidden;
  position: relative;
  border-radius: 10px;
  img {
    height: 64px;
    width: calc(100% + 6px);
    position: absolute;
    top: -2px;
    left: -3px;
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
